package org.example.os.synchronous.producer_comsumer;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * BlockingQueue
 */
public class BlockingQueueAchievement {
    private final BlockingQueue<Integer> queue;

    public BlockingQueueAchievement(int capacity){
        this.queue = new LinkedBlockingQueue<>(capacity);
    }

    public void produce() throws InterruptedException {
        int value = 0;
        while (true) {
            System.out.println("生产者生产:" + value);
            queue.put(value ++);
            Thread.sleep(1000);
        }
    }

    public void consume() throws InterruptedException {
        while (true) {
            int value = queue.take();
            System.out.println("消费者消费:" + value);
            Thread.sleep(1000);
        }
    }
}
